package com.atguigu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author tangsy
 * @Description TODO
 * @date 2022/9/29 16:28
 * @Version 1.0
 */
@Configuration
@EnableWebSecurity //@EnableWebSecurity是开启SpringSecurity的默认行为
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //允许iframe嵌套显示
        http.headers().frameOptions().disable();
        //登录设置
        http
                .authorizeRequests()
                .antMatchers("/static/**","/loginPage").permitAll()  //允许匿名用户访问的路径
                .anyRequest().authenticated();   // 其它页面全部需要验证
        http
                .formLogin()
                .loginPage("/loginPage") //表单页面的地址
                .usernameParameter("username") // 如果省略，必须是username
                .passwordParameter("password") //如果省略，必须是password
                .loginProcessingUrl("/login")  //提交表单的处理地址
                //.failureForwardUrl("/login") //登录失败后哪里
                .defaultSuccessUrl("/");//登录成功后哪里
        http
                .logout()
                .logoutUrl("/logout" ) //退出登陆的路径，指定spring security拦截的注销url,退出功能是security提供的
                .invalidateHttpSession(true)
                .logoutSuccessUrl("/loginPage");//用户退出后要被重定向的url
        http
                .csrf().disable();//关闭跨域请求伪造功能

        http
                .exceptionHandling()
                .accessDeniedHandler(new CustomAccessDeineHandler());
    }

//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication()
//                .withUser("lucy")
//                .password(new BCryptPasswordEncoder().encode("123456"))
//                .roles("admin","manager");
//
//        auth.inMemoryAuthentication()
//                .withUser("happy")
//                .password(new BCryptPasswordEncoder().encode("111111"))
//                .roles("");
//    }

//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        //必须调用父类的方法，否则就不需要认证即可访问
//        super.configure(http);
//        //允许iframe嵌套显示
//        //http.headers().frameOptions().disable();
//        //允许iframe显示
//        http.headers().frameOptions().sameOrigin();
//    }

}